//拦截器

const baseURL = 'https://obuge.com/juzao'

let interceptors = {///拦截器
  request(req){//请求拦截器
    return new Promise(next =>{
      req.url = /^http/.test(req.url) ? req.url : (baseURL + req.url)
      req.header = req.header || {}
      //设置token
      if(wx.token) req.header.token = wx.token
      next(req)
    })
  },
  response(res){///响应拦截器
    return new Promise(next => {
      let data = res.data
      if(res.statusCode == 404){
        data = {errorMsg:'请求接口不存在'}
        // next({errorMsg:'请求接口不存在'})
      }else if(res.statusCode == 500){
        data = {errorMsg:res.data || '服务器错误，请联系网站管理员'}
        // next({errorMsg:res.data || '服务器错误，请联系网站管理员'})
      }else if(res.statusCode == 200){///正常业务
        // next(res.data)
      }else{
        data = {errorMsg:res.errMsg || '未知错误'}
        // next({errorMsg:res.errMsg || '未知错误'})
      }
      if(data.errorMsg){
        wx.showToast({
          title: data.errorMsg,
          icon: 'none',
          duration: 2000
        })
      }
      next(data)
    })
  }
}

function GET(url,query,args){
  args = args || {}
  return new Promise((resolve,reject) => {
    interceptors.request({url,...args}).then(req =>{
      wx.request({
        ...req,
        // url:/^http/.test(url) ? url : (baseURL + url),
        // url: baseURL + url,
        data:query,
        success: (res) => {
          interceptors.response(res).then(data => {
            resolve(data)
          })
        },
        fail:(res) => {
          interceptors.response(res).then(data => {
            resolve(data)
          })
        }
      })
    })
    
  })
}

function PUT(url,data,args){
  args = args || {} 
  return new Promise((resolve,reject) => {
    interceptors.request({url,...args}).then(req =>{
      wx.request({
        ...req,
        // url:/^http/.test(url) ? url : (baseURL + url),
        // url: baseURL + url,
        data,
        method:'PUT',
        success: (res) => {
          interceptors.response(res).then(data => {
            resolve(data)
          })
        },
        fail:(res) => {
          interceptors.response(res).then(data => {
            resolve(data)
          })
        }
      })
    })
    
  })
}

function POST(url,data,args){
  args = args || {}
  return new Promise((resolve,reject) => {
    interceptors.request({url,...args}).then(req =>{
      wx.request({
        ...req,
        // url:/^http/.test(url) ? url : (baseURL + url),
        // url: baseURL + url,
        data,
        method:'POST',
        success: (res) => {
          interceptors.response(res).then(data => {
            resolve(data)
          })
        },
        fail:(res) => {
          interceptors.response(res).then(data => {
            resolve(data)
          })
        }
      })
    })
    
  })
}

function DELETE(url,data){
  return new Promise((resolve,reject) => {
    interceptors.request({url}).then(req =>{
      wx.request({
        ...req,
        // url:/^http/.test(url) ? url : (baseURL + url),
        // url: baseURL + url,
        data,
        method:'delet',
        success: (res) => {
          interceptors.response(res).then(data => {
            resolve(data)
          })
        },
        fail:(res) => {
          interceptors.response(res).then(data => {
            resolve(data)
          })
        }
      })
    })
    
  })
}

module.exports = {
  baseURL,
  get: GET,
  put: PUT,
  post: POST,
  delete: DELETE
}



// const baseURL = 'https://obuge.com/juzao'
// let interceptors = {//拦截器
//   response(res){//响应拦截器
//     return new Promise(next=>{
//       if(res.statusCode == 404){
//         next({errorMsg:'请求接口错误'})
//       }else if(res.statusCode == 500){
//         next({errorMsg:res.data || '服务器错误，请联系网站管理员'})
//       }else if(res.statusCode == 200){ 
//         next(res.data)
//       }else{
//         next({errorMsg:res.errMsg || '未知错误'})
//       }
      
//     })

//   }
// }

// function get(url,query){
//   return new Promise((resolve,reject) =>{
//     wx.request({   //从服务器上获取轮播图
//       url: baseURL + url, //接口地址
//       data:query,
//       success: (res)=> {
//         interceptors.response(res).then(data=>{
//           resolve(data)
//         })
//         // console.log(res,res)
//         // resolve(res ? res : {errorMsg: res.errMsg || '请求错误'})
//       },
//       fail:(res) =>{
//         interceptors.response(res).then(data=>{
//           resolve(data)
//         })
//       }  
//     })
//   })
// }

// module.exports = {
//   baseURL,
//   get
// }
